//
//  UserSericesChildVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserSericesChildVC.h"
#import "UserGroup.h"
#import "UserItem.h"
#import "UserSaveItem.h"
#import "UserCenterCell.h"
#import "UserSeriesViewModel.h"
#import "UserPersonalDetailVC.h"
#import "UserBrandController.h"
#import "UserEquipController.h"
#import "UserTennisLineVC.h"

@interface UserSericesChildVC ()
/**
 *  当前选中的cell
 */
@property (nonatomic, strong)UserCenterCell *currentCell;
@end

@implementation UserSericesChildVC {
    
    UserSeriesViewModel *_seriesViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _seriesViewModel = [UserSeriesViewModel shareInstance];
    
    if (self.childType == SericesChildCity) {
        [self loadCitesData];
    }
    
}

// 加载省份对应城市数据
- (void)loadCitesData {

    if (self.childType == SericesChildCity) {
        self.content = _seriesViewModel.selectProvices;
        
        UserGroup *group0 = [[UserGroup alloc] init];
        group0.headHeight = kAutoHei(24);
        
        NSArray *cities = [_seriesViewModel citiesWithProvinces:self.content];
        
        for (NSString *city in cities) {
            UserSaveItem *item = [[UserSaveItem alloc] initWithTitle:city];
            [group0.items addObject:item];
        }
        
        [self.groups addObject:group0];
        
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserSaveItem *item = group.items[indexPath.row];
    cell.item = item;
    
    if (self.currentCell == nil && [item.title isEqualToString:self.content]) {
        
        self.currentCell = cell;
        cell.selected = YES;
        [cell setSelected:YES animated:NO];
        
    }
    
    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block();
        return;
    }
    
    UserCenterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.currentCell) {
        self.currentCell.selected = NO;
    }
    
    cell.selected = YES;
    self.currentCell = cell;
    
    [self didSelecthandle];
    
}

- (void)didSelecthandle {
    
    for (UIViewController *vc in self.navigationController.childViewControllers) {
        if ([vc isKindOfClass:[UserPersonalDetailVC class]] && self.childType == SericesChildCity) {
            [self.navigationController popToViewController:vc animated:YES];
            
              [[NSNotificationCenter defaultCenter] postNotificationName:userChangeAdressNotification object:[NSString stringWithFormat:@"%@ %@",self.content, self.currentCell.item.title]];
            
        }else if ([vc isKindOfClass:[UserEquipController class]] && self.childType == SericesChildBrandAndModle){
            
            [self.navigationController popToViewController:vc animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:userChangeUserEquipNotification object:nil userInfo:@{@"Brand":self.content, @"Model":self.currentCell.item.title}];
            
            
        }else if ([vc isKindOfClass:[UserTennisLineVC class]] && self.childType == SericesChildTennisLineBrandAndModle){
            [self.navigationController popToViewController:vc animated:YES];

            [[NSNotificationCenter defaultCenter] postNotificationName:userChangeUserTennisLineEquipNotification object:nil userInfo:@{@"Brand":self.content, @"Model": self.currentCell.item.title}];
        }
    }

}


@end
